package lego.jbv;

import java.util.ArrayList;

import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;

public class BumperController extends Thread{
	private TouchSensor left;
	private TouchSensor right;
	private boolean stopNow = false;
	private static ArrayList<String> messages;
	
	public BumperController(ArrayList<String> messages, SensorPort leftBumper, SensorPort rightBumper)
	{
		this.messages = messages;
		left = new TouchSensor(leftBumper);
		right = new TouchSensor(rightBumper);
		this.setDaemon(true);
	}
	
	public void run() {
		while(!stopNow)
		{
			if(left.isPressed() || right.isPressed())
			{
				addHit();
				
				while(left.isPressed() || right.isPressed())
				{
					Thread.yield();
				}
				
				Display.setStatus("");
			}
			
			Thread.yield();
		}
	}
	
	public void stop()
	{
		stopNow = true;
	}
	
	private synchronized void addHit()
	{
		if(!messages.contains(String.valueOf(BTCommand.HIT)))
			messages.add(String.valueOf(BTCommand.HIT));
	}
}
